package com.bjbn.bean;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 监控响应实体类
 * 
 * @author bjbn
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MonitorResponse {
    
    /**
     * 响应状态码
     */
    private Integer code;
    
    /**
     * 响应消息
     */
    private String message;
    
    /**
     * 当前时间
     */
    private LocalDateTime currentTime;
    
    /**
     * Topic信息列表
     */
    private List<TopicInfo> topics;
    
    /**
     * 统计信息
     */
    private Statistics statistics;
    
    /**
     * 统计信息内部类
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Statistics {
        /**
         * 总Topic数量
         */
        private Integer totalTopics;
        
        /**
         * 正常Topic数量
         */
        private Integer normalTopics;
        
        /**
         * 延迟Topic数量
         */
        private Integer delayedTopics;
        
        /**
         * 无数据Topic数量
         */
        private Integer noDataTopics;
        
        /**
         * 总消息数量
         */
        private Long totalMessages;
    }
    
    public MonitorResponse(Integer code, String message, LocalDateTime currentTime, List<TopicInfo> topics) {
        this.code = code;
        this.message = message;
        this.currentTime = currentTime;
        this.topics = topics;
        this.statistics = calculateStatistics(topics);
    }
    
    private Statistics calculateStatistics(List<TopicInfo> topics) {
        if (topics == null || topics.isEmpty()) {
            return new Statistics(0, 0, 0, 0, 0L);
        }
        
        int total = topics.size();
        int normal = 0;
        int delayed = 0;
        int noData = 0;
        long totalMessages = 0;
        
        for (TopicInfo topic : topics) {
            if (topic.getMessageCount() != null) {
                totalMessages += topic.getMessageCount();
            }
            
            if ("NORMAL".equals(topic.getStatus())) {
                normal++;
            } else if ("DELAYED".equals(topic.getStatus())) {
                delayed++;
            } else if ("NO_DATA".equals(topic.getStatus())) {
                noData++;
            }
        }
        
        return new Statistics(total, normal, delayed, noData, totalMessages);
    }
} 